<?php


namespace app\crm\logic;


use app\admin\logic\CommonLogic;
use app\admin\model\Setting;

class MessageLogic extends CommonLogic
{

    /**
     * 菜单标识
     * @var \string[][]
     */
    protected  $menuList = [
           'prep_common_leads_list'     =>  [ 'text' => '共%s条线索','type' => 'leads','name' => '待进入公海的线索','class' => LeadsLogic::class,'menu_tag' => 'LeadsList','identifier' => 'prep_common_leads_num'],
           'overtime_business_list'     =>  [ 'text' => '共%s条商机','type' => 'business','name' => '超时未跟进商机','class' => BusinessLogic::class,'menu_tag' => 'Business','identifier' => 'overtime_business_num'],
           'today_leads_list'           =>  [ 'text' => '共%s条线索','type' => 'leads','name' => '今日分配的线索','class' => LeadsLogic::class,'menu_tag' => 'LeadsList','identifier' => 'today_leads_num'],
           'today_customer_list'        =>  [ 'text' => '共%s个客户','type' => 'customer','name' => '今日分配的客户','class' => CustomerLogic::class,'menu_tag' => 'Customer','identifier' => 'today_customer_num'],
           'today_business_list'        =>  [ 'text' => '共%s条商机','type' => 'business','name' => '今日分配的商机','class' => BusinessLogic::class,'menu_tag' => 'Business','identifier' => 'today_business_num'],
           'progress_order_list'        =>  [ 'text' => '共%s个订单','type' => 'order','name' => '进行中的订单','class' => OrderLogic::class,'menu_tag' => 'OrderForm','identifier' => 'progress_order_num'],
           'unpayment_order_list'       =>  [ 'text' => '共%s个订单','type' => 'order','name' => '未回款的订单','class' => OrderLogic::class,'menu_tag' => 'OrderForm','identifier' => 'unpayment_order_num'],
           'audit_sample_list'          =>  [ 'text' => '共%s条试样','type' => 'sample','name' => '审批中的试样申请','class' => SampleLogic::class,'menu_tag' => 'ForetasteList','identifier' => 'audit_sample_num']
        ];


    function __construct()
    {
        /*
        $this->menuList['prep_common_leads_list']['logo'] = $this->menuList['today_leads_list']['logo'] = config('app.img_url').config('app.app_icon')['vertical']['LeadsList'];
        $this->menuList['today_customer_list']['logo'] = config('app.img_url').config('app.app_icon')['vertical']['Customer'];
        $this->menuList['overtime_business_list']['logo'] =  $this->menuList['today_business_list']['logo'] = config('app.img_url').config('app.app_icon')['vertical']['Business'];
        $this->menuList['audit_sample_list']['logo'] = config('app.img_url').config('app.app_icon')['vertical']['ForetasteList'];
        $this->menuList['unpayment_order_list']['logo'] = $this->menuList['progress_order_list']['logo'] = config('app.img_url').config('app.app_icon')['vertical']['OrderForm'];

        */
        }


    /**
     * 获取配置内容
     * @author Mr.Li
     * @create 2021-08-10 15:20
     */
    protected function getSettingConfig()
    {
        $configRes = [];
        $config = (new Setting())->getConfigByGroupName('admin_customer_set');
        if($config){
            foreach($config as $key => $value)
            {
                $configRes[$value['name']] = $value['value'];
            }
        }
        return $configRes;
    }


    /**
     * 根据菜单类型获取参数
     * @param $menuType
     * @param $param
     * @param $config
     * @author Mr.Li
     * @create 2021-08-13 13:54
     */
    protected function getMergeParamsByMenuType($menuType,$param,$config)
    {
        $mergeParams = $param;
        if($menuType == 'prep_common_leads_list'){
            if(isset($config['switch']) && $config['switch']){
                $mergeParams['un_finish_time'] = $config['leads_time'];
            }else{
                $mergeParams['un_finish_time'] = -1;
            }
        }
        if($menuType == 'today_leads_list'){
            $mergeParams['is_today_allocate'] = 1;
        }
        if($menuType == 'overtime_business_list'){
            $mergeParams['overtime'] = $config['business_time'] ?? -1;
        }
        if($menuType == 'today_business_list'){
            $mergeParams['is_today_allocate'] = 1;
        }
        if($menuType == 'unpayment_order_list'){
            $config['return_money_status'] = $config['return_money_status'] ?? -1;
            if($config['return_money_status'] == 4){
                $config['return_money_status'] = $config['return_money_time'] ?? -1;
            }
            $mergeParams['return_money_status'] =$config['return_money_status'] ?? -1;
        }
        if($menuType == 'progress_order_list'){
            $mergeParams['is_progress'] = 1;
        }
        return $mergeParams;

    }
    /**
     * 根据权限获取用户数量
     * @param $userId
     * @param $param
     * @param $permission
     * @author Mr.Li
     * @create 2021-08-09 14:30
     */
    public function getNum($userId,$param,$permission)
    {
        $client = new ClientLogic();
        $res = [];
        $config = $this->getSettingConfig();
        in_array('*',$permission,true) ? $isSuper = true : $isSuper = false;
        foreach($this->menuList as $menuList => $menuData){
            $menu = $menuData['menu_tag'];
            if(isset($permission[$menu]) || $isSuper) {
                if( $isSuper || $permission[$menu]){
                    $mergeParams = $this->getMergeParamsByMenuType($menuList,$param,$config);
                    $num = $client->getMessageNum($menuData['class'] ,$userId,$mergeParams);
                    $menuData['text'] =  sprintf( $menuData['text'], $num);
                    $res[] =  [
                        'name'          => $menuData['name'],
                        'text'          =>  $menuData['text'],
                        'num'           =>  $num,
                        'msg_type'      => $menuList,
                        'module_type'   => $menuData['type'],
                    ];
                }
            }

        }
        return $res;

    }

    /**
     * 获取消息列表内容
     * @param $userId
     * @param $param
     * @param $permission
     * @author Mr.Li
     * @create 2021-08-10 11:24
     */
    public function getList($userId,$param,$permission)
    {
        $param['limit'] = $param['limit'] ?? 10;
        $param['page'] = $param['page'] ?? 1;
        $client = new ClientLogic();
        $res = [];
        if(isset($this->menuList[$param['type']])){
            $config = $this->getSettingConfig();
            $mergeParams = $this->getMergeParamsByMenuType($param['type'],$param,$config);
            $res = $client->getMessageList($this->menuList[$param['type']]['class'] ,$userId,$mergeParams);
        }
        return $res;

    }

}
